/*
 * @Author: yuanming
 * @Date: 2024-06-30 08:01:12
 * @LastEditors: yuanming
 * @LastEditTime: 2024-09-13 10:28:24
 * @Description: fileDescription
 */
import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import viteCompression from 'vite-plugin-compression'
// import { importToCDN } from 'vite-plugin-external-cdn'
// import { visualizer } from 'rollup-plugin-visualizer'

import createVitePlugins from './vite/plugins'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd())
    return {
        // 部署生产环境和开发环境下的URL。
        base: './',
        plugins: [
            viteCompression(),
            ...createVitePlugins(env, command),
            // importToCDN({
            //     modules: [
            //         {
            //             name: 'Element Plus',
            //             var: 'ElementPlus',
            //             path: 'https://unpkg.com/element-plus@2.7.6/dist/index.full.js'
            //         }, {
            //             name: 'ECharts',
            //             var: 'echarts',
            //             path: 'https://unpkg.com/echarts@5.5.1/dist/echarts.min.js'
            //         }, {
            //             name: 'moment',
            //             var: 'moment',
            //             path: 'https://unpkg.com/moment@2.30.1/min/moment.min.js'
            //         }, {
            //             name: 'vue',
            //             var: 'Vue',
            //             path: 'https://unpkg.com/vue@3.4.31/dist/vue.global.prod.js'
            //         }
            //     ]
            // }),
            // visualizer({
            //     open: true,
            //     gzipSize: true,
            //     brotliSize: true
            // })
        ],
        resolve: {
            alias: {
                // 设置路径
                '~': path.resolve(__dirname, './'),
                // 设置别名
                '@': path.resolve(__dirname, './src'),
            },
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
        },
        // vite 相关配置
        server: {
            port: 8900,
            host: true,
            open: true,
        },
        build: {
            assetsInlineLimit: 4096, // 小于4k的静态资源内联
            terserOptions: {
                // 压缩配置
                compress: {
                    drop_console: true, // 生产环境移除console
                    drop_debugger: true // 生产环境移除debugger
                }
            },
            rollupOptions: {
                output: {
                    // manualChunks(id) {
                    //     if (id.includes('node_modules')) {
                    //         return id.toString().split('node_modules/')[1].split('/')[0].toString()
                    //     }
                    // },
                    entryFileNames: 'assets/js/[name]-[hash].js',
                    chunkFileNames: 'assets/js/[name]-[hash].js',
                    assetFileNames(assetInfo) {
                        if (assetInfo.name.endsWith('.css')) {
                            return 'assets/css/[name]-[hash].css'
                        }
                        if (['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp'].some((ext) => assetInfo.name.endsWith(ext))) {
                            return 'assets/images/[name]-[hash].[ext]'
                        }
                        if (['.woff', '.TTF', '.ttf', '.OTF'].some((ext) => assetInfo.name.endsWith(ext))) {
                            return 'assets/fonts/[name]-[hash].[ext]'
                        }
                        return 'assets/[name]-[hash].[ext]'
                    },
                },
            }
        },
    }
})
